package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by siba on 22/09/14.
 */
public class UserInput {

    public static String readUserInput (){
        String userInputStr;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
            userInputStr = br.readLine();
            if(userInputStr.equalsIgnoreCase("quit"))
                return Constants.QUIT_STRING;
        }catch (IOException e) {
            new BibliotecaMessages(System.out).printMessage(Constants.NOT_VALID_MESSAGE);
            return Constants.NOT_VALID_MESSAGE;
        }
        return userInputStr;
    }


    public static String [] getBookDetailsFromUser() {
        String [] bookDetails = new String[Constants.MESSAGES_TO_ASK_FOR_BOOK_ATTRIBUTES.length];
        String input = Constants.NOT_VALID_MESSAGE;

        for (int count = 0; count < Constants.MESSAGES_TO_ASK_FOR_BOOK_ATTRIBUTES.length; count++){
            while(input.equalsIgnoreCase(Constants.NOT_VALID_MESSAGE)){
                new BibliotecaMessages(System.out).printMessage(Constants.MESSAGES_TO_ASK_FOR_BOOK_ATTRIBUTES[count]);
                input = readUserInput();
            }
            bookDetails[count] = input;
            input = Constants.NOT_VALID_MESSAGE;
        }
        return bookDetails;
    }

    public static String[] getMovieDetailsFromUser() {
        String [] movieDetails = new String[Constants.MESSAGES_TO_ASK_FOR_MOVIE_ATTRIBUTES.length];
        String input = Constants.NOT_VALID_MESSAGE;

        for (int count = 0; count < Constants.MESSAGES_TO_ASK_FOR_MOVIE_ATTRIBUTES.length; count++){
            while(input.equalsIgnoreCase(Constants.NOT_VALID_MESSAGE)){
                new BibliotecaMessages(System.out).printMessage(Constants.MESSAGES_TO_ASK_FOR_MOVIE_ATTRIBUTES[count]);
                input = readUserInput();
            }
            movieDetails[count] = input;
            input = Constants.NOT_VALID_MESSAGE;
        }
        return movieDetails;
    }

    public String[] getUserLoginDetails() {
        String [] userDetails = new String[Constants.MESSAGES_TO_ASK_FOR_USER_LOGIN_DETAILS.length];
        String input = Constants.NOT_VALID_MESSAGE;

        for (int count = 0; count < Constants.MESSAGES_TO_ASK_FOR_USER_LOGIN_DETAILS.length; count++){
            while(input.equalsIgnoreCase(Constants.NOT_VALID_MESSAGE)){
                new BibliotecaMessages(System.out).printMessage(Constants.MESSAGES_TO_ASK_FOR_USER_LOGIN_DETAILS[count]);
                input = readUserInput();
            }
            userDetails[count] = input;
            input = Constants.NOT_VALID_MESSAGE;
        }
        return userDetails;
    }
}
